package cn.jdy.plugins.wap;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.socket.TextMessage;

import cn.jdy.authority.entity.User;
import cn.jdy.authority.service.UserService;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.safe.SafeUtil;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.Chat;
import cn.jdy.plugins.entity.Live;
import cn.jdy.plugins.service.LineService;
import cn.jdy.plugins.service.LiveService;
import cn.jdy.plugins.websocket.WebsocketEndPoint;


/**
 * @title 导报管理
 * @author jga
 * @since 3017-3-30
 */

@Controller
@RequestMapping("/guide")
public class GuideCtrl extends BaseCtrl{
	@Autowired
	private LiveService LiveService;
	@Autowired
	private UserService userService;
	/**
	 * 进入导播首页
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("{id}.html")
	public ModelAndView edits(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/guide/index.jsp");
		WebsocketEndPoint wp=new WebsocketEndPoint();
		
		mv.addObject("rs", wp.getOnlineCount());
		mv.addObject("data", id==null||id==0 ? id : this.LiveService.find(id));
		return mv;
	}
	
	/**
	 * 修改直播信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("updateLive")
	@ResponseBody
	public Map<String, Object> edits(Live live) {
	Map<String, Object> jsonMap = new HashMap<String, Object>();{
			try {
			Live	le=this.LiveService.find(live.getId());
			le.setStatus(live.getStatus());
			le.setLiveurl(live.getLiveurl());
			WebsocketEndPoint wp=new WebsocketEndPoint();
			this.LiveService.update(le);
			//给客户端发信息 chatType 是6 控制视频的播放地址
//			String json = "{liveId:"+le.getId()+", chatType:'6',status:'"+live.getStatus()+"',liveurl:'"+live.getLiveurl()+"',moviurl:'"+live.getMoviurl()+"',backmoviurl:'"+live.getBackmoviurl()+"'}";
			String json = ""+le.getId()+",6,"+le.getStatus()+","+live.getLiveurl()+","+le.getMoviurl()+","+le.getBackmoviurl()+"";
	    	TextMessage textMessage = new TextMessage(json);
	    	wp.sendMessageToUsers(textMessage);
			jsonMap.put("statusCode", "200");
			} catch(Exception e) {
				jsonMap.put("statusCode", "400");
				BaseLog.e(getClass(), "删除异常！", e);
			}
	}
	return jsonMap;
	}	  
	/**
	 * 修改直播信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("updateLive1")
	@ResponseBody
	public Map<String, Object> edits1(Long id,String liveurl, String status) {
	Map<String, Object> jsonMap = new HashMap<String, Object>();{
			try {
			String liveurl1="http:"+liveurl;
			Live	le=this.LiveService.find(id);
			le.setStatus(status);
			le.setLiveurl(liveurl1);
			WebsocketEndPoint wp=new WebsocketEndPoint();
			this.LiveService.update(le);
			//给客户端发信息 chatType 是6 控制视频的播放地址
//			String json = "{liveId:"+le.getId()+", chatType:'6',status:'"+live.getStatus()+"',liveurl:'"+live.getLiveurl()+"',moviurl:'"+live.getMoviurl()+"',backmoviurl:'"+live.getBackmoviurl()+"'}";
			String json = ""+le.getId()+",6,"+le.getStatus()+","+liveurl1+","+le.getMoviurl()+","+le.getBackmoviurl()+"";
	    	TextMessage textMessage = new TextMessage(json);
	    	wp.sendMessageToUsers(textMessage);
			jsonMap.put("statusCode", "200");
			} catch(Exception e) {
				jsonMap.put("statusCode", "400");
				BaseLog.e(getClass(), "删除异常！", e);
			}
	}
	return jsonMap;
	}	
	/**
	 * 修改前台直播状态
	 * 
	 * @param liveid  ，listatus ,uname,ups
	 * @return
	 */
	@RequestMapping("editLievzt")
	@ResponseBody
	public Map<String, Object> editLievzt(Long id, String lstatus,String liveurl,String uname,String ups) {
	Map<String, Object> jsonMap = new HashMap<String, Object>();{
			try {
			
				User user = userService.findOne("username", uname);// 查询用户是否存在
		  		if(user == null) {
		  			jsonMap.put("statusCode", "400");
		  		} else if(!user.getPasswd().equals(ups)) {
		  				jsonMap.put("statusCode", "400");
		  			}else if(user.getPasswd().equals(ups)) {
		  				Live	le=this.LiveService.find(id);
		  				le.setStatus(lstatus);
		  			//	le.setLiveurl(liveurl);
		  				WebsocketEndPoint wp=new WebsocketEndPoint();
		  				this.LiveService.update(le);
		  				//给客户端发信息 chatType 是6 控制视频的播放地址
		  				String json = ""+le.getId()+",6,"+le.getStatus()+","+liveurl+","+le.getMoviurl()+","+le.getBackmoviurl()+"";
		  		    	TextMessage textMessage = new TextMessage(json);
		  		    	wp.sendMessageToUsers(textMessage);
		  				jsonMap.put("statusCode", "200");
		  			}
			
			} catch(Exception e) {
				jsonMap.put("statusCode", "400");
				BaseLog.e(getClass(), "删除异常！", e);
			}
	}
	return jsonMap;
	}	
	
	/**
	 * 修改前台直播状态
	 * 
	 * @param liveid  ，listatus ,uname,ups
	 * @return
	 */
	@RequestMapping("editLievSatus")
	@ResponseBody
	public Map<String, Object> editLiveSatus(Long liveid, String status,String uname,String passwd) {
	Map<String, Object> jsonMap = new HashMap<String, Object>();{
			try {
			
				User user = userService.findOne("username", uname);// 查询用户是否存在
		  		if(user == null) {
		  			jsonMap.put("statusCode", "400");
		  		} else if(!user.getPasswd().equals(passwd)) {
		  				jsonMap.put("statusCode", "400");
		  			}else if(user.getPasswd().equals(passwd)) {
		  				Live le = LiveService.find(liveid);
		  				if (le != null) {
		  					le.setStatus(status);
		  					le.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
						LiveService.update(le);
		  				le.setStatus(status);
		  				WebsocketEndPoint wp=new WebsocketEndPoint();
		  				this.LiveService.update(le);
		  				//给客户端发信息 chatType 是6 控制视频的播放地址
		  				String json = ""+le.getId()+",6,"+le.getStatus()+","+le.getLiveurl()+","+le.getMoviurl()+","+le.getBackmoviurl()+"";
		  		    	TextMessage textMessage = new TextMessage(json);
		  		    	wp.sendMessageToUsers(textMessage);
		  				jsonMap.put("statusCode", "200");
		  				}
		  			}
			
			} catch(Exception e) {
				jsonMap.put("statusCode", "400");
				BaseLog.e(getClass(), "删除异常！", e);
			}
	}
	return jsonMap;
	}	  
}
